﻿using MediatR;
using WMS.API.Application.Command.BasicManagement.ProductManage;
using WMS.Domain.Entities.BasicManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.BasicManagement.ProductManage
{
    public class DelProductManageCommandHandler : IRequestHandler<DelProductManageCommand, APIResult<int>>
    {
        /// <summary>
        /// 产品仓储
        /// </summary>
        private readonly IBaseRepository<ProductManageModel> _productManageModelRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productManageModelRepository"></param>
        public DelProductManageCommandHandler(IBaseRepository<ProductManageModel> productManageModelRepository)
        {
            _productManageModelRepository = productManageModelRepository;
        }
        /// <summary>
        /// 产品删除命令处理
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(DelProductManageCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var i = await _productManageModelRepository.DeleteAsync(request.Id);
                return await Task.FromResult(i > 0 ? new APIResult<int>("产品信息删除成功") { Code = ResultCode.Success } : new APIResult<int>("产品信息删除失败") { Code = ResultCode.Failed });
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
